package com.miao.shop.controller;

import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Operation;
import com.miao.common.entity.pojo.ShopOrder;
import com.miao.shop.entity.vo.OrderVO;

import com.miao.shop.service.OrderService;
import com.miao.shop.service.ShopCarService;
import com.miao.shop.service.ShopOperationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.text.html.Option;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Miaowenqing
 * @date 2021-02-01 16:30
 * @description 订单相关接口
 */
@RestController
@RequestMapping("/shop/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private ShopOperationService shopOperationService;






    @PostMapping("/submit")
    public R submit(@RequestBody OrderVO orderVO){
        /**
         * 生成编号00的OrderId
         */
        String orderId = System.currentTimeMillis()+"00"+(new Random().nextInt(90000)+10000);

        List<ShopOrder> orders = orderService.splitOrder(orderId, orderVO);

        if (this.orderService.saveBatch(orders)) {



            return R.ok(orderId);
        }
        return R.error();


    }
    @GetMapping("/getProInfo")
    public R getProInfo(String oId){
        String proInfo  = orderService.getProInfo(oId);
        R r = new R();
        r.setCode(2000);
        r.setData(proInfo);
        return r;
    }
    @GetMapping("/getMyOrder")

    public R getMyOrder(int cId,int state){
    return R.ok(orderService.getMyOrder(cId,state));
    }


    @GetMapping("/getMyAllOrder")
    public R getMyAllOrder(int cId){
        return R.ok(orderService.getMyOrder(cId));
    }


    @GetMapping("/getUnpay")
    public R getUnpay(int cId){

        List<Object> unpay = orderService.getUnpay(cId);
        return R.ok(unpay);
    }
    @GetMapping("/getCurrentUnpay")
    public R getCurrentUnpay(String key){
        Object currentUnpay = orderService.getCurrentUnpay(key);
        if (null==currentUnpay) {
            return R.error();
        }
        return R.ok(currentUnpay);
    }


    /**
     * 用户收货 操作
     * @param oId oid
     * @return R
     */
    @GetMapping("/receipt")
    public  R receipt(String oId){





        Operation operation = new Operation();
        operation.setOrderId(oId);
        operation.setOperationDes("用户签收");
        operation.setOperationTime(new Date());

        this.shopOperationService.save(operation);
        if (this.orderService.updateReceive(oId)>0) {
            return R.ok();
        }
        return R.error();
    }






}

